package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.criteria.OrderInfoCriteria;
import com.ayf.payment.game.api.dto.platform.MerchantSettleDTO;
import com.ayf.payment.game.api.dto.platform.criteria.MerchantBalanceRecordCriteria;
import com.ayf.payment.game.api.entity.MerchantBalanceRecord;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantBalanceRecordMapper;
import com.ayf.payment.game.api.mapper.MerchantMapper;
import com.ayf.payment.game.api.mapper.MerchantPayOrderMapper;
import com.ayf.payment.game.api.mapper.platform.MerchantCashOutMapper;
import com.ayf.payment.game.api.service.MerchantBalanceRecordService;
import com.ayf.payment.game.api.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MerchantBalanceRecordServiceImpl implements MerchantBalanceRecordService {

    @Resource
    private MerchantBalanceRecordMapper merchantBalanceRecordMapper;

    @Resource
    private MerchantMapper merchantMapper;

    @Resource
    private MerchantCashOutMapper merchantCashOutMapper;

    @Resource
    private MerchantPayOrderMapper merchantPayOrderMapper;

    @Override
    public List<MerchantBalanceRecord> select(MerchantBalanceRecordCriteria criteria) {

        return merchantBalanceRecordMapper.selectBudgets(criteria);
    }

    @Override
    public MerchantBalanceRecord selectToday(Integer merchantId) {
        if (merchantId == null) {
            throw new ParameterException("商户id不能为空");
        }
        OrderInfoCriteria orderCriteria = new OrderInfoCriteria();
        Date date = new Date();
        String today = DateUtils.dateToString(date, DateUtils.formatPattern);
        orderCriteria.setStartTime(today + " 00:00:00");
        orderCriteria.setDate(DateUtils.dateToInteger(date));
        orderCriteria.setMerchantId(merchantId);
        Map<Integer, Map<String, Object>> withdrawalMap = merchantCashOutMapper.selectSumWithdrawal(orderCriteria);
        Map<Integer, Map<String, Object>> payMap = merchantPayOrderMapper.selectSumPay(orderCriteria);

        BigDecimal withdrawalAmount = BigDecimal.ZERO;
        if (withdrawalMap.get(merchantId) != null) {
            withdrawalAmount = withdrawalAmount.add((BigDecimal) withdrawalMap.get(merchantId).get("merchantTotalWithdrawal"));
        }
        if (payMap.get(merchantId) != null) {
            withdrawalAmount = withdrawalAmount.add((BigDecimal) payMap.get(merchantId).get("merchantTotalPay"));
        }
        MerchantBalanceRecord merchantBalanceRecord = new MerchantBalanceRecord();
        merchantBalanceRecord.setWithdrawalAmount(withdrawalAmount);
        merchantBalanceRecord.setSettleDate(DateUtils.dateToString(new Date(), DateUtils.formatPattern));
        List<MerchantSettleDTO> settleList = merchantMapper.getSettleList(DateUtils.dateToInteger(date), merchantId);
        merchantBalanceRecord.setCashWithdrawal(settleList.get(0).getMerchantSettleAmount());
        if (settleList.get(0).getYesterdaySettleAmount() == null) {
            merchantBalanceRecord.setSettleAmount(BigDecimal.ZERO);
        } else {
            merchantBalanceRecord.setSettleAmount(settleList.get(0).getYesterdaySettleAmount());
        }
        return merchantBalanceRecord;
    }

    private MerchantBalanceRecord getMerchantBalanceRecord(Integer merchantId, String today, Map<Integer, Map<String, Object>> tradeMap, Map<Integer, Map<String, Object>> proxyMap, Map<Integer, Map<String, Object>> settleMap, Map<Integer, Map<String, Object>> withdrawalMap, Map<Integer, Map<String, Object>> payMap, Map<Integer, Map<String, Object>> balanceMap) {
        BigDecimal tradeAmount = BigDecimal.ZERO;
        BigDecimal withdrawalAmount = BigDecimal.ZERO;
        BigDecimal settleAmount = BigDecimal.ZERO;
        BigDecimal balance = BigDecimal.ZERO;
        if (tradeMap.get(merchantId) != null) {
            tradeAmount = tradeAmount.add((BigDecimal) tradeMap.get(merchantId).get("merchantTotalIncome"));
        }
        if (proxyMap.get(merchantId) != null) {
            tradeAmount = tradeAmount.add((BigDecimal) proxyMap.get(merchantId).get("proxyTotalIncome"));
        }
        if (settleMap.get(merchantId) != null) {
            settleAmount = settleAmount.add((BigDecimal) settleMap.get(merchantId).get("merchantTotalSettle"));
        }
        if (withdrawalMap.get(merchantId) != null) {
            withdrawalAmount = withdrawalAmount.add((BigDecimal) withdrawalMap.get(merchantId).get("merchantTotalWithdrawal"));
        }
        if (payMap.get(merchantId) != null) {
            withdrawalAmount = withdrawalAmount.add((BigDecimal) payMap.get(merchantId).get("merchantTotalPay"));
        }
        if (balanceMap.get(merchantId) != null) {
            balance = balance.add((BigDecimal) balanceMap.get(merchantId).get("balance"));
        }
        MerchantBalanceRecord merchantBalanceRecord = new MerchantBalanceRecord();
        merchantBalanceRecord.setTradeAmount(tradeAmount);
        merchantBalanceRecord.setWithdrawalAmount(withdrawalAmount);
        merchantBalanceRecord.setSettleAmount(settleAmount);
        merchantBalanceRecord.setCashWithdrawal(balance.add(settleAmount).subtract(withdrawalAmount));
        merchantBalanceRecord.setSettleDate(today);
        merchantBalanceRecord.setMerchantId(merchantId);
        return merchantBalanceRecord;
    }

    @Override
    public boolean addBudgets(List<Integer> subList, Map<Integer, Map<String, Object>> tradeMap, Map<Integer, Map<String, Object>> proxyMap, Map<Integer, Map<String,
            Object>> settleMap, Map<Integer, Map<String, Object>> withdrawalMap, Map<Integer, Map<String, Object>> payMap) {
//        List<MerchantBalanceRecord> list = new ArrayList<>();
        String date = DateUtils.dateToString(DateUtils.getDayBeforeDay(1), DateUtils.formatPattern);
        Map<Integer, Map<String, Object>> balanceMap = merchantBalanceRecordMapper.selectBalance(date, subList);
        for (Integer merchantId : subList) {
            MerchantBalanceRecord merchantBalanceRecord1 = getMerchantBalanceRecord(merchantId, date, tradeMap, proxyMap, settleMap, withdrawalMap, payMap, balanceMap);
            merchantBalanceRecordMapper.insertSelective(merchantBalanceRecord1);
//            list.add(merchantBalanceRecord1);
        }
//        return merchantBalanceRecordMapper.insertBudgets(list) > 0;
        return true;
    }
}
